import schedule from "node-schedule";
import clearLog from "./clearLog.js";
import clearCode from "./clearCode.js"

class ScheduleList {
  constructor() {
    this.list = new Set();
  }

  createSchedule(time, callback) {
    if (this.list.has(time)) return false;
    try {
      this.list.add(time);
      schedule.scheduleJob(time, callback);
      return true;
    } catch (error) {
      console.log(`创建定时任务 ${time} 失败: ${error.message}`);
    }
  }

  deleteSchedule(time) {
    if (!this.list.includes(time)) return false;
    schedule.cancelJob(time);
    this.list.delete(time);
    return true;
  }
}

// 创建定时任务列表实例
const scheduleList = new ScheduleList();
scheduleList.createSchedule(clearLog.timer, clearLog.fn);
scheduleList.createSchedule(clearCode.timer, clearCode.fn);

// 导出定时任务列表实例，定时任务取消时使用
export default scheduleList;
